import { apiWrapper } from '@/utils/request'

// ==================== 用户端银行卡接口 ====================

// 获取当前用户的银行卡列表
export const getUserCards = () => apiWrapper.get('/User/user/cards')

// 获取银行卡详情
export function getCardDetail(cardId) {
  return apiWrapper.get(`/User/card/${cardId}`)
}

// 根据卡号查询余额
export const getCardBalance = (cardId) => apiWrapper.get(`/User/cards/${cardId}`)

// 存款
export const deposit = (data) => apiWrapper.post('/User/cards/deposit', data)

// 取款
export const withdraw = (data) => apiWrapper.post('/User/cards/withdraw', data)

// 转账
export const transfer = (data) => apiWrapper.post('/User/cards/transfer', data)

// 挂失银行卡
export const reportLoss = (data) => apiWrapper.put('/User/cards/reportLoss', data)

// 修改银行卡密码
export const changeCardPassword = (data) => apiWrapper.put('/User/cards/changedPwd', data)

// ==================== 管理员端银行卡接口 ====================

// 获取所有银行卡（管理员）
export function getCardsByAdmin(params) {
  return apiWrapper.get('/Admin/card/admin', { params })
}

// 根据客户ID获取银行卡
export function getCardsByCustomer(customerId) {
  return apiWrapper.get(`/Admin/card/customer/${customerId}`)
}

// 新增银行卡
export const addCard = (data) => apiWrapper.post('/Admin/card/addCard', data)

// 更新银行卡密码（管理员）
export const adminChangeCardPassword = (data) => apiWrapper.put('/Admin/card/changePwd', data)

// 获取银行卡状态
export const getCardStatus = (cardId) => apiWrapper.get(`/Admin/card/getStatus/${cardId}`)

// 更新银行卡状态
export const changeCardStatus = (data) => apiWrapper.put('/Admin/card/changeStatus', data)

// 删除银行卡
export const deleteCard = (cardId) => apiWrapper.delete(`/Admin/card/deleteCard/${cardId}`)

// 获取银行卡统计信息
export function getCardStats() {
  return apiWrapper.get('/Admin/card/stats')
}

// 根据银行卡号获取银行卡信息
export const getCardInfo = (cardId) => apiWrapper.get(`/Admin/card/getCard/${cardId}`)

// 根据客户身份证获取银行卡列表
export const getCardsByPid = (pid) => apiWrapper.get(`/Admin/card/getCardsByPid/${pid}`)

// 范围分页查询银行卡
export const pageCard = (data, begin, end) => {
  const params = new URLSearchParams()
  if (begin) params.append('begin', begin)
  if (end) params.append('end', end)
  
  const url = `/Admin/card/pageCard${params.toString() ? '?' + params.toString() : ''}`
  
  return apiWrapper.post(url, data)
}

// ========================= 银行卡操作API (/User/cards) =========================
export const cardApi = {
  // 存款
  deposit: (data) => apiWrapper.post('/User/cards/deposit', data),

  // 取款
  withdraw: (data) => apiWrapper.post('/User/cards/withdraw', data),

  // 转账
  transfer: (data) => apiWrapper.post('/User/cards/transfer', data),

  // 查询银行卡余额
  getBalance: (cardId) => apiWrapper.get(`/User/cards/${cardId}`),

  // 挂失银行卡
  reportLoss: (data) => apiWrapper.put('/User/cards/reportLoss', data),

  // 修改银行卡密码
  changePassword: (data) => apiWrapper.put('/User/cards/changedPwd', data),
}

// ========================= 管理员银行卡管理API (/Admin/card) =========================
export const adminCardApi = {
  // 新增银行卡
  addCard: (cardData) => {
    return apiWrapper.post('/Admin/card/addCard', cardData)
  },

  // 更新银行卡密码
  changePwd: (pwdData) => {
    return apiWrapper.put('/Admin/card/changePwd', pwdData)
  },

  // 获取银行卡状态
  getStatus: (cardid) => {
    return apiWrapper.get(`/Admin/card/getStatus/${cardid}`)
  },

  // 更新银行卡状态
  changeStatus: (statusData) => {
    return apiWrapper.put('/Admin/card/changeStatus', statusData)
  },

  // 删除银行卡
  deleteCard: (cardid) => {
    return apiWrapper.delete(`/Admin/card/deleteCard/${cardid}`)
  },

  // 根据银行卡号获取银行卡信息
  getCard: (cardid) => {
    return apiWrapper.get(`/Admin/card/getCard/${cardid}`)
  },

  // 根据客户身份证获取银行卡列表
  getCardsByPid: (pid) => {
    return apiWrapper.get(`/Admin/card/getCardsByPid/${pid}`)
  },

  // 分页查询银行卡
  pageCard: (queryData, begin, end) => {
    // 构建正确的请求体，匹配CardQueryDTO，避免null值导致后端NPE
    const requestBody = {
      cardid: queryData.cardid || '',
      curid: queryData.curid || '',
      savingid: queryData.savingid || 0,
      isreportloss: queryData.isreportloss || '',
      customername: queryData.customername || '',
      page: queryData.page || 1,
      pageSize: queryData.pageSize || 20
    }

    // 构建查询参数
    const params = new URLSearchParams()
    if (begin) params.append('begin', begin)
    if (end) params.append('end', end)

    const url = `/Admin/card/pageCard${params.toString() ? '?' + params.toString() : ''}`
    
    return apiWrapper.post(url, requestBody)
  }
}

// 兼容旧版本的导出
export const depositToCard = cardApi.deposit
export const withdrawFromCard = cardApi.withdraw
export const transferBetweenCards = cardApi.transfer 